package com.noname.lib_base_java.mvp;

import android.annotation.SuppressLint;

import com.noname.lib_base_java.entity.FielBean;
import com.noname.lib_base_java.net.BaseApi;
import com.noname.lib_base_java.net.NetManager;
import com.noname.lib_base_java.net.RtManager;
import com.noname.lib_base_java.net.RxHelper;
import com.noname.lib_base_java.net.download.DownloadInfo;
import com.noname.lib_base_java.net.download.DownloadManager;
import com.noname.lib_base_java.util.L;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * @author:Lmoumou
 * @date:2018/12/9 描述:
 **/
public abstract class BaseModelImpl implements BaseContract.BaseModel {

    @Override
    public void doHttp(Observer<ResponseBody> observer, Observable<ResponseBody> observable) {
        L.e("LLSSQQ", "doHttp");
        observable.compose(RxHelper.<ResponseBody>rxSchedulerHelper())
                .subscribe(observer);
    }

    /**
     * 多个文件上传
     */
    @Override
    public void mUpLoadFiel(Observer<ResponseBody> observer, List<FielBean> fielList) {
        ArrayList<MultipartBody.Part> params = new ArrayList<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multippart/form-data"), NetManager.getInstance().getAreaFiel());
        for (FielBean fielBean : fielList) {
            if (fielBean.isLocal) {
                File file = new File(fielBean.path);
                RequestBody requestFiel = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestFiel);
                params.add(part);
            }
        }
        String url = NetManager.getInstance().getResUrl() + "upload/files";
        Observable<ResponseBody> observable = RtManager.getInatance().createApi(BaseApi.class).upLoadFiels(url, requestBody, params);
        observable.compose(RxHelper.<ResponseBody>rxSchedulerHelper())
                .subscribe(observer);

    }

    /**
     * 单个文件上传
     */
    @Override
    public void mUpLoadFiel(Observer<ResponseBody> observer, FielBean fielBean) {
        RequestBody requestPath = RequestBody.create(MediaType.parse("multippart/form-data"), NetManager.getInstance().getAreaFiel());
        File file = new File(fielBean.path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        String url = NetManager.getInstance().getResUrl() + "upload/files";
        Observable<ResponseBody> observable = RtManager.getInatance().createApi(BaseApi.class).upLoadFiel(url, requestPath, part);
        observable.compose(RxHelper.<ResponseBody>rxSchedulerHelper())
                .subscribe(observer);
    }

    @SuppressLint("CheckResult")
    @Override
    public void mDownload(Observer<DownloadInfo> observer, DownloadInfo info, DownloadManager.ProgressListener progressObserver) {
        DownloadManager downloadManager = DownloadManager.getInstance();
        downloadManager.setProgressListener(progressObserver);
        downloadManager.start(observer, info);
    }

    @Override
    public void mPause() {
        DownloadManager.getInstance().pause();
    }
}
